﻿//
///// 调用本页面前，请先引用jquery.js文件
//
var form_action = '#';

function verify_req(obj) {
    var msg = '必填';

    if ($(obj).val().length == 0) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

function verify_email(obj) {
    var reg = /^([^@\u4e00-\u9fa5])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    var msg = '邮件格式错误';

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

function verify_tel(obj) {
    var reg = /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
    var msg = '座机格式错误';

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}
// 校验登录名：只能输入3-16个以字母开头、可带数字、“_”、“.”的字串 
function verify_loginnames(obj) {
    var reg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){3,15}$/;
    var msg = '登录名格式错误';

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}
// 校验激活码：只能输入字母、数字、“-”的字串 
function verify_activecode(obj) {
    var reg = /^[0-9a-zA-Z-,]+$/;
    var msg = '激活码格式错误';

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}
//校验用户姓名：只能输入2-6个以中文的字串  
function verify_truename(obj) {
    var reg = /^[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*$/;
    var msg = '姓名格式错误';

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}
//验证银行卡：卡号必须为15位或16位或19位数字

function verify_trueacct(obj) {
    var reg = /^(\d{15}|\d{16}|\d{19})$/;
    var msg = '银行卡号格式错误';

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

//验证手机号码：13、15、18
function verify_phones(obj) {
    var reg = /^1\d{10}$/;
    var msg = '手机格式错误';

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}
//验证电话或手机号码：13、14、15、18或021-1234567
function verify_telphones(obj) {
    var reg = /^1\d{10}$/;
    var msg = '联系方式格式错误';
    var regtel = /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val()) && !regtel.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}
function verify_pwd(obj) {
    var msg = '密码长度最小为6位';

    if( isEmpty(obj) ){return};
    if ($(obj).val().length < 6) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

function verify_int(obj) {
    var msg = '必须为整数';
    var reg = /^-?(([1-9]\d*)|(0))$/;

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

function verify_qq(obj) {
    var msg = '长度大于5，必须为数字';
    var reg = /^[1-9]\d{4,11}$/;

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

function verify_degree(obj) {
    var msg = '必须为整数，0到9999';
    var reg = /^(([1-9]\d{0,3})|(0))$/;

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val()) || parseInt($(obj).val()) > 9999) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

function verify_float(obj) {
    var msg = '必须为数字';
    var reg = /^-?[0-9]+(\.)?([0-9])?([0-9])?$/;

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val())) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

function verify_money(obj) {
    var msg = '必须为数字，且不能大于9999999';
    var reg = /^-?[0-9]+(\.[0-9]?([0-9]))?$/;

    if( isEmpty(obj) ){return};
    if (!reg.test($(obj).val()) || parseFloat($(obj).val()) > 9999999) set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

//身份证号码：请正确输入您的身份证号（15/18位数字或字母）。
function verify_cradid(obj) {
    var msg = '请正确输入您的身份证号';

    if( isEmpty(obj) ){return};
    if (!checkIdentityCard($(obj).val()))
        set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}
function verify_sensitive(obj) {
    var msg = '内容包含不允许内容';
    var objval = $(obj).val();
    if (objval != "") {
        $.ajax({
            dataType: 'jsonp',
            jsonp: "callback",
            url: '//api.dd373.com/shop/checkContent',
            data: { content: $(obj).val(), t: Math.random() },
            success: function (res) { if (res != "true") set_flag_verify(obj, msg, false); else set_flag_verify(obj, msg, true); },
            async: false
        });
    }
}
//相等
function verify_eq(obj) {
    var msg = '确认信息必须一致';
    var arrj = $("input[type=" + $(obj).attr("type") + "]");
    var index = arrj.index($(obj));
    var prev = $(arrj.get(index - 1));
    
    if( isEmpty(obj) ){return};
    if ($(obj).val() == "" || $(obj).val() != prev.val())
        set_flag_verify(obj, msg, false);
    else set_flag_verify(obj, msg, true);
}

function set_flag_verify(obj, msg, value) {

    $("span[id='msg_" + $(obj).attr('name') + "']").remove();
    if ($(obj).attr('msg') != undefined && $(obj).attr('msg') != null && $(obj).attr('msg').length > 0)
        msg = $(obj).attr('msg');

    if (value == false)//$(obj).after("<span style='color:red' id='msg_" + $(obj).attr('name') + "'> " + msg + "</span>");
    {
        $(obj).addClass("inputError");
    }
    else {
        $(obj).removeClass("inputError");
    }

}
// 验证非必填选项值是否为空
function isEmpty(obj){
    if(obj.val()!=""){ 
        return false;
    }else{
        return true;
    }
}

///obj ---> 表单的name值
function verfy_submit() {
    var obj = '.form:visible';
    $(obj + " .req:not(:hidden)").each(function (i, v) {
        verify_req($(v));
    });
    $(obj + " .loginnames:not(:hidden)").each(function (i, v) {
        verify_loginnames($(v));
    });
    $(obj + " .activecode:not(:hidden)").each(function (i, v) {
        verify_activecode($(v));
    });
    $(obj + " .truename:not(:hidden)").each(function (i, v) {
        verify_truename($(v));
    });
    $(obj + " .email:not(:hidden)").each(function (i, v) {
        verify_email($(v));
    });
    $(obj + " .tel:not(:hidden)").each(function (i, v) {
        verify_tel($(v));
    });
    $(obj + " .trueacct:not(:hidden)").each(function (i, v) {
        verify_trueacct($(v));
    });
    $(obj + " .phones:not(:hidden)").each(function (i, v) {
        verify_phones($(v));
    });
    $(obj + " .telphones:not(:hidden)").each(function (i, v) {
        verify_telphones($(v));
    });
    $(obj + " .icard:not(:hidden)").each(function (i, v) {
        verify_cradid($(v));
    });

    $(obj + " .pwd:not(:hidden)").each(function (i, v) {
        verify_pwd($(v));
    });

    $(obj + " .int:not(:hidden)").each(function (i, v) {
        verify_int($(v));
    });

    $(obj + " .money:not(:hidden)").each(function (i, v) {
        verify_money($(v));
    });
    $(obj + " .cradid:not(:hidden)").each(function () {
        verify_cradid($(this));
    });
    $(obj + ".orcaid:not(:hidden)").each(function () {
        if ($(this).val() != "") {
            verify_cradid($(this));
        }
        else set_flag_verify($(this), "", true);

    });
    $(obj + " .sensitive:not(:hidden)").each(function () {
        if ($(this).val() != "") verify_sensitive($(this));
    });
    $(obj + " .degree:not(:hidden)").each(function () {
        if ($(this).val() != "") verify_degree($(this));
    });
    $(obj + " .qqno:not(:hidden)").each(function () {
        if ($(this).val() != "") verify_qq($(this));
    });

    $(obj + " .eq:not(:hidden)").each(function (i, v) {
        verify_eq($(v));
    });
    $(obj + " .hide_phone:not(:hidden)").each(function () {
        if ($(this).val() != "" && $(this).val().indexOf("*") == -1) verify_phones($(this));
        else { set_flag_verify($(this), "", true) }
    });
    $(obj + " .hide_qq:not(:hidden)").each(function () {
        if ($(this).val() != "" && $(this).val().indexOf("*") == -1) verify_qq($(this));
        else { set_flag_verify($(this), "", true) }
    });
    return $(".inputError:not(:hidden)").length == 0 ? true : false;
}

function verfy_AddInfosubmit() {
    var obj = '.form:visible';
    $(obj + " .loginnames:not(:hidden)").each(function (i, v) {
        verify_loginnames($(v));
    });
    $(obj + " .email:not(:hidden)").each(function (i, v) {
        verify_email($(v));
    });
    return $(".inputError:not(:hidden) ").length == 0 ? true : false;
}

$(function () {
    NVerifyStart();
});
function NVerifyStart() {
    $(".req:not(:hidden)").blur(function () {
        verify_req($(this));
    });
    $(".hide_phone,.hide_qq").focus(function () {
        if ($(this).val() == $(this).attr("def")) {
            $(this).val("");
        }
    });
    $(".hide_phone,.hide_qq").keydown(function (e) {
        var key_code = e.keyCode;
        console.log(key_code)
        if (key_code == 8
            || (key_code >= 48 && key_code <= 57)
            || (key_code >= 96 && key_code <= 105)) {//8后退键,48-57大,96-105小键盘
            return true;
        } else {
            return false;
        }
    });
    $(".hide_phone").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("def"));
            set_flag_verify($(this), "", true);
        } else {
            verify_phones($(this));
        }
    });
    $(".hide_qq").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("def"));
            set_flag_verify($(this), "", true);
        } else {
            verify_qq($(this));
        }
    });
    $(".loginnames:not(:hidden)").blur(function () {
        verify_loginnames($(this));
    });
    $(".activecode:not(:hidden)").blur(function () {
        verify_activecode($(this));
    });
    $(".truename:not(:hidden)").blur(function () {
        verify_truename($(this));
    });
    $(".email:not(:hidden)").blur(function () {
        verify_email($(this));
    });
    $(".trueacct:not(:hidden)").blur(function () {
        verify_trueacct($(this));
    });
    $(".tel:not(:hidden)").blur(function () {
        verify_tel($(this));
    });
    $(".icard:not(:hidden)").blur(function () {        
        verify_cradid($(this));
    });
    $(".phones:not(:hidden)").blur(function () {
        verify_phones($(this));
    });
    $(".telphones:not(:hidden)").blur(function () {
        verify_telphones($(this));
    });
    $(".pwd:not(:hidden)").blur(function () {
        verify_pwd($(this));
    });

    $(".int:not(:hidden)").blur(function () {
        verify_int($(this));
    });

    $(".money:not(:hidden)").blur(function () {
        verify_money($(this));
    });
    $(".cradid:not(:hidden)").blur(function () {
        verify_cradid($(this));
    });
    $(".orcaid:not(:hidden)").blur(function () {
        if ($(this).val() != "") {
            verify_cradid($(this));
        }
        else set_flag_verify($(this), "", true);
    });
    $(".sensitive:not(:hidden)").blur(function () {
        verify_sensitive($(this));
    });
    $(".degree:not(:hidden)").blur(function () {
        verify_degree($(this));
    });
    $(".qqno:not(:hidden)").blur(function () {
        verify_qq($(this));
    });
    $(".eq:not(:hidden)").blur(function () {
        verify_eq($(this));
    });
    $(".form").submit(function () {
        var checkres = verfy_submit();
        if (!checkres)
            alert("请检查页面上红色边框的值是否符合电话，数字，邮箱等格式");
        return checkres;
    });
}


//去除空格和HTml
function KongAndNoHTml(objText) {
    objText = objText.replace(/<[^>].*?>/g, "").replace(/(^\s*)|(\s*$)/g, '');
    return objText;
}

/*2:32 2011-5-8 身份证*/
function checkIdentityCard(identityCardNumber) { var idPattern = /^\d{15}(\d{2}[\dxX])?$/; if (!idPattern.test(identityCardNumber)) return false; var errorAddressCode = /^[09]/; if (errorAddressCode.test(identityCardNumber)) return false; var result = false; if (identityCardNumber.length == 15) result = validateIdentityCard15(identityCardNumber); else result = validateIdentityCard18(identityCardNumber); var realAge = getAge(identityCardNumber); return result && realAge > 0 } var m_quan = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var m_verifyCode = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"); function validateIdentityCard18(identityCardNumber) { var sum = 0; for (i = 0; i < 17; i++) sum += parseInt(identityCardNumber.charAt(i), 10) * m_quan[i]; var mod = sum % 11; var vc = m_verifyCode[mod]; return vc.toUpperCase() == identityCardNumber.charAt(17).toUpperCase(); }
function validateIdentityCard15(identityCardNumber) {
    identityCardNumber = identityCardNumber.substr(0, 6) + "19" + identityCardNumber.substr(6, 9); var year = identityCardNumber.substr(6, 4); var month = identityCardNumber.substr(10, 2); var day = identityCardNumber.substr(12, 2); var tempDate = year + "/" + month + "/" + day; var birthday = new Date(Date.parse(tempDate.replace(/-/g, "/"))); if (isNaN(birthday)) return false; var isRight = false; for (num = 0; num < m_verifyCode.length; num++) {
        var tempId = identityCardNumber + m_verifyCode[num]; isRight =
validateIdentityCard18(tempId); if (isRight) break
    } return isRight;
}
function getAge(identityCardNumber) {
    var realAge = 0; var birthdayValue; if (15 == identityCardNumber.length) { birthdayValue = identityCardNumber.charAt(6) + identityCardNumber.charAt(7); birthdayValue = "19" + birthdayValue; birthdayValue = birthdayValue + "/" + identityCardNumber.charAt(8) + identityCardNumber.charAt(9) + "/" + identityCardNumber.charAt(10) + identityCardNumber.charAt(11) } if (18 == identityCardNumber.length) birthdayValue = identityCardNumber.charAt(6) + identityCardNumber.charAt(7) + identityCardNumber.charAt(8) + identityCardNumber.charAt(9) +
"/" + identityCardNumber.charAt(10) + identityCardNumber.charAt(11) + "/" + identityCardNumber.charAt(12) + identityCardNumber.charAt(13); if (!validateDate(birthdayValue)) return 0; var dt2 = new Date; var dt1 = new Date(Date.parse(birthdayValue.replace(/-/g, "/"))); realAge = dt2.getFullYear() - dt1.getFullYear(); return realAge;
}
function validateDate(date) {
    var year, month, day;
    year = parseInt(date.substr(0, 4), 10);
    month = parseInt(date.substr(5, 2), 10);
    day = parseInt(date.substr(8, 2), 10);
    if (year == 0 || month == 0 || day == 0)
        return false;
    if (year < 1930)
        return false;
    var isRight = false;
    switch (month) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12: isRight = day >= 1 && day <= 31; break;
        case 4: case 6: case 9: case 11: isRight = day >= 1 && day <= 30; break;
        case 2: if (isLeapYear(year)) { isRight = day >= 1 && day <= 29; break } else { isRight = day >= 1 && day <= 28; break }
        default: break
    }
    return isRight;
}
function isLeapYear(pYear) { var leapYear = pYear % 4 == 0 && pYear % 100 != 0 || pYear % 100 == 0 && pYear % 400 == 0; return leapYear } function isManhood(identityCardNumber) { return getAge(identityCardNumber) >= 18; };
function checkLength(obj, name, max) {
    var maxChars = max;
    var ch_length = obj.value.length;
    for (var i = Math.floor(maxChars / 2) ; i < ch_length; i++) {
        var content = obj.value.substring(0, i);
        if (content.replace(/([\u0391-\uFFE5])/ig, '11').length >= maxChars) {
            obj.value = obj.value.substring(0, i);
            document.getElementById("ls_" + name).innerHTML = curr.toString(0);
            break;
        }
    }
    test = obj.value.replace(/([\u0391-\uFFE5])/ig, '11');
    var curr = maxChars - test.length;
    document.getElementById("ls_" + name).innerHTML = curr.toString();
}
function checkLengthnormal(obj, name, max) {
    var curr = max - obj.value.length;
    document.getElementById("ls_" + name).innerHTML = curr.toString();
}
